package com.xukehong.frame.security;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xukehong.frame.entity.Role;
import com.xukehong.frame.entity.User;
import com.xukehong.frame.entity.UserRole;
import com.xukehong.frame.mapper.RoleMapper;
import com.xukehong.frame.mapper.UserMapper;
import com.xukehong.frame.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 许克弘
 * @date 2020/2/26 18:42
 */
@Service
public class MyUserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //从数据库根据用户名获取用户信息
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attr.getRequest();
        String password = request.getParameter("password");
        System.out.println("账号:" + s + "密码:" + password);
        User user = new User();
        user.setUsername(s);
        User userByName = userMapper.selectOne(user);
        UserDetails userDetails = null;
        if (userByName != null) {
            if (!new BCryptPasswordEncoder().matches(password, userByName.getPassword())) {
                throw new BadCredentialsException("密码错误");
            }
            //创建一个集合来存放权限
            Collection<GrantedAuthority> authorities = getAuthorities(user);
            //实例化UserDetails对象
            userDetails = new org.springframework.security.core.userdetails.User(s, userByName.getPassword(), true, true, true, true, authorities);
        } else {
            throw new BadCredentialsException("用户不存在");
        }
        return userDetails;
    }

    private Collection<GrantedAuthority> getAuthorities(User user) {
        List<GrantedAuthority> authList = new ArrayList<>();
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getUserId());
        UserRole resultUserRole = userRoleMapper.selectOne(userRole);
        Role role = roleMapper.selectById(resultUserRole.getRoleId());
        authList.add(new SimpleGrantedAuthority(role.getName()));
        return authList;
    }

}
